package uiLayer;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

import ctrLayer.GraphCtr;
import ctrLayer.PresetCtr;
import modelLayer.Flavour;
import modelLayer.GraphContainer;
import modelLayer.PresetContainer;

public class PresetUI extends MenuUI{
	private Menu menu;
	private boolean exit;
	
	public PresetUI(){
		try{
			menu = new Menu("Preset UI", PresetUI.class);
			menu.add("Save Current Graph as Preset", "saveCurrentAsPreset");
			menu.add("Delete Preset", "deletePreset");
			menu.add("List Presets", "listPresets");
			menu.add("Load Preset Into Graph", "loadPreset");
			menu.add("Back", "back");
			
			presetMenu();
		} catch(Exception e){
			e.printStackTrace();
		}
	}
	
	private void presetMenu() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		exit = false;
		while(!exit){
			Method m = menu.run();
			if(m != null){
				m.invoke(this, null);
			}
		}
	}
	
	@SuppressWarnings("unused")
	private void back(){
		exit = true;
	}
	
	private void save(){
		PresetCtr pCtr = new PresetCtr();
		try {
			pCtr.save();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("unused")
	private void saveCurrentAsPreset(){
		try{
			GraphContainer gCont = GraphContainer.getInstance();
			PresetContainer pCont = PresetContainer.getInstance();
			ArrayList<Flavour> preset = gCont.getFlavours();
			pCont.addPreset(preset);
			
			String s = "";
			for(Flavour f : preset){
				s += f.getFlavour() + ", ";
			}
			System.out.println(s.substring(0, s.length()-2) + " added as Preset");
			save();
			pause();
		}catch(NullPointerException e){
			System.out.println(e.getMessage());
			pause();
			return;
		}
	}
	
	@SuppressWarnings("unused")
	private void deletePreset(){
		try{
			Scanner keyboard = new Scanner(System.in);
			clearConsole();
			System.out.println("*** Delete Preset ***");
			PresetCtr pCtr = new PresetCtr();
			ArrayList<ArrayList<Flavour>> presets = pCtr.getPresets();
			
			StringBuilder sb = new StringBuilder();
			sb.append("(");
			for(int i = 0; i < presets.size(); i++){
				sb.append(i + ", ");
			}
			sb.replace(sb.length()-2, sb.length(), ")");
			
			System.out.print("Preset ID" + sb.toString() + ": ");
			int id = keyboard.nextInt();
			keyboard.nextLine();
			if(confirm("Do you wish to delete the following preset: " + id)){
				pCtr.removePreset(id);
				System.out.println("ID " + id + " removed!");
				save();
			}
		}catch(NullPointerException e){
			System.out.println(e.getMessage());
			pause();
			return;
		}catch(IndexOutOfBoundsException e){
			System.out.println("That ID does not exist!");
			pause();
			return;
		}
	}
	
	@SuppressWarnings("unused")
	private void listPresets(){
		try{
			clearConsole();
			System.out.println("*** Presets ***");
			PresetCtr pCtr = new PresetCtr();
			ArrayList<ArrayList<Flavour>> presets = pCtr.getPresets();
			if(presets != null && !presets.isEmpty()){
				int i = 0;
				for(ArrayList<Flavour> preset : presets){
					StringBuilder sb = new StringBuilder();
					sb.append(i + " - ");
					for(Flavour f : preset){
						sb.append(f.getFlavour() + ", ");
					}
					sb.delete(sb.length()-2, sb.length());
					System.out.println(sb.toString());
					i++;
				}
				pause();
			}else{
				System.out.println("There are no Presets yet!");
				pause();
			}
		}catch(NullPointerException e){
			System.out.println(e.getMessage());
			pause();
			return;
		}
	}
	
	@SuppressWarnings("unused")
	private void loadPreset(){
		try{
			Scanner keyboard = new Scanner(System.in);
			PresetCtr pCtr = new PresetCtr();
			ArrayList<ArrayList<Flavour>> presets = pCtr.getPresets();
			
			StringBuilder sb = new StringBuilder();
			sb.append("(");
			for(int i = 0; i < presets.size(); i++){
				sb.append(i + ", ");
			}
			sb.replace(sb.length()-2, sb.length(), ")");
			
			System.out.print("Preset ID" + sb.toString() + ": ");
			int id = keyboard.nextInt();
			keyboard.nextLine();
			
			ArrayList<Flavour> preset = pCtr.getPreset(id);
			
			GraphCtr gCtr = new GraphCtr();
			gCtr.clear();
			
			for(Flavour f : preset){
				gCtr.addToGraph(f.getFlavour());
				System.out.println(f.getFlavour() + " - Loaded");
			}
			
			Skeleton s = Skeleton.getInstance();
			s.reDraw();
			s.setVisible(true);
			pause();
			
		}catch(InputMismatchException e){
			System.out.println("Invalid values");
			pause();
			return;
		}catch(NullPointerException e){
			System.out.println(e.getMessage());
			pause();
			return;
		}
	}
}
